package org.example.servicelogin.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.example.model.manage.bean.Result;
import org.example.model.manage.bean.po.security.User;
import org.example.model.manage.bean.vo.UserAuthRequest;
import org.example.servicelogin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理", description = "用户登录、注册、登出等接口")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户登录", description = "用户通过账号密码登录系统")
    @PostMapping("/login")
    public Result login(@RequestBody UserAuthRequest request) {
        if(request.getAccount() == null || request.getPassword() == null) {
            return Result.error(10006, "参数错误");
        }

        String JWT = userService.login(request.getAccount(), request.getPassword());

        return Result.success(JWT);
    }

    @Operation(summary = "用户登出", description = "用户退出登录")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        userService.logout(request.getHeader("Authorization"));
        return Result.success(null);
    }

    @Operation(summary = "获取用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping("/info")
    public Result info(){
        return Result.success(userService.getUserInfo());
    }

}
